package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import com.itheima.utils.UserVoUtils;
import com.itheima.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    MenuService menuService;
    @Reference
    UserService userService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return userService.findPage(queryPageBean);
    }

    @RequestMapping("/findById.do")
    private Result findById(Integer userId) {
        try {
            User user = userService.findById(userId);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }
    @RequestMapping("/findrolesByUserId.do")
    private Result findrolesByUserId(Integer id) {
        try {
           List<Integer> list = userService.findrolesByUserId(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds) {
        try {
            if(user.getPassword().length()<20){
            String pwd = bCryptPasswordEncoder.encode(user.getPassword());
            user.setPassword(pwd);
            }
            userService.edit(user, roleIds);

            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody User user, Integer[] roleIds) {
        try {

            String pwd = bCryptPasswordEncoder.encode(user.getPassword());
            user.setPassword(pwd);
            userService.userAdd(user, roleIds);
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {

            userService.delete(id);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }

    //获得当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername() {
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        User byUsername = userService.findByUsername(user.getUsername());

        List<UserVo> userVo = UserVoUtils.getUserVo(byUsername);
        Map map = new HashMap();
        map.put("menuList", userVo);
        if (user != null) {
            String username = user.getUsername();
            map.put("username", username);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, map);
        }

        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    /**查找所有的健康管理师用户
     * @param
     * @return com.itheima.entity.Result
     * @author JANME
     * @date 2020-09-24 20:42
     */
    @RequestMapping("/findAllHeathUser")
    public Result findAllHeathUser() {
        try {
            List<com.itheima.pojo.User> userList =  userService.findAllHeathUser();
            return new Result(true,MessageConstant.GET_USERHEALTH_SUCCESS,userList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_USERHEALTH_FAIL);
        }
    }


}
